package com.yang.crawler.thread.factory;


import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadMonitorFactory implements ThreadFactory {
    private String poolName;

    private String prefixName;

    private static final AtomicInteger poolNumber = new AtomicInteger(0);

    private AtomicInteger threadNumber = new AtomicInteger(0);

    public ThreadMonitorFactory(String poolName) {
        this.poolName = poolName;
        this.prefixName = this.poolName + "-pool-" + poolNumber.incrementAndGet() + "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(this.prefixName + threadNumber.incrementAndGet());
        return thread;
    }
}
